import createApi from './createApi'
import encrypt from './encrypt'
import configs from '../../server/config'

export default createApi({
  // 动态设置headers
  getHeaders: config => {
    const { params = {}, data = {}, headers: userHeaders } = config
    const now = Date.parse(new Date()) / 1000
    const headers = {
      'X-Halo-App': 'wedding-hotel-h5',
      'X-Halo-Region': 310100,
      'Http-Request-Halo-Time': now,
      'X-Halo-Source': 'l6vB6n',
      'X-Halo-Visitor': '', // visitor.visitor_id
      'Http-Request-Halo-Sign': encrypt({ ...params, ...data, time: now }),
      ...userHeaders
    }
    return headers
  },
  handleError: (status, msg, error) => console.log(status, msg, error), // 自定义错误处理
  createOptions: {
    baseURL: process.browser ? '' : configs.proxy
  }
})
